//
//  UploadArea.m
//  kankanVideo
//
//  Created by wangbo on 6/15/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "UploadArea.h"
#import "AppDelegate.h"
#import <MediaPlayer/MediaPlayer.h>

@implementation UploadArea
@synthesize videoAdress;
- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    firstView=[[UIView alloc]initWithFrame:frame];
    [self addSubview:firstView];
    [firstView release];
    secondView=[[UIView alloc]initWithFrame:frame];
    [self addSubview:secondView];
    [secondView release];
    [secondView setHidden:YES];
    NSLog(@"upload_area_height:%@",self.frame.size.height);
    thumbnailView=[[UIImageView alloc]initWithFrame:CGRectMake(130-50, 60-37, 100, 75)];
    [thumbnailView setBackgroundColor:[UIColor blueColor]];
    [secondView addSubview:thumbnailView];
    [thumbnailView release];
    btnDel=[UIButton buttonWithType:UIButtonTypeCustom];
    [btnDel setBackgroundImage:[UIImage imageNamed:@"Fever.png"] forState:UIControlStateNormal];
    [btnDel addTarget:self action:@selector(btnBackTapped:) forControlEvents:UIControlEventTouchUpInside];
    [btnDel setFrame:CGRectMake(70, 13, 20, 20)];
    [secondView addSubview:btnDel];
    
    btnSelect=[UIButton buttonWithType:UIButtonTypeCustom];
    [btnSelect setBackgroundImage:[UIImage imageNamed:@"exsistedvideo.png"] forState:UIControlStateNormal];
    [btnSelect addTarget:self action:@selector(selectVideo:) forControlEvents:UIControlEventTouchUpInside];
    [btnSelect setFrame:CGRectMake(30, 10, 70, 70)];
    lblSelect=[[UILabel alloc]initWithFrame:CGRectMake(30, 85, 70, 25)];
    lblSelect.text=@"选择视频";
    [firstView addSubview:btnSelect];
    [firstView addSubview:lblSelect];
    [lblSelect release];
    
    btnTake=[UIButton buttonWithType:UIButtonTypeCustom];
    [btnTake setBackgroundImage:[UIImage imageNamed:@"takevideo.png"] forState:UIControlStateNormal];
    [btnTake addTarget:self action:@selector(takeVideo:) forControlEvents:UIControlEventTouchUpInside];
    [btnTake setFrame:CGRectMake(160, 10, 70, 70)];
    lblTake=[[UILabel alloc]initWithFrame:CGRectMake(160, 85, 70, 25)];
    lblTake.text=@"拍摄视频";
    [firstView addSubview:btnTake];
    [firstView addSubview:lblTake];
    [lblTake release];
    
    return self;
}
-(void)btnBackTapped:(id)sender{
    thumbnailView.image=nil;
    [firstView setHidden:NO];
    [secondView setHidden:YES];
}
-(void)getThumbnailFromVideo:(NSURL *)videourl atTime:(NSTimeInterval)playbackTime{
    NSLog(@"%@",videourl);
    MPMoviePlayerController *mpctrl=[[MPMoviePlayerController alloc]initWithContentURL:videourl];
    //mpctrl.shouldAutoplay = NO;
    mpctrl.initialPlaybackTime = playbackTime;
    mpctrl.currentPlaybackTime = playbackTime;
    //thumbnailView = [[UIImageView alloc]initWithImage:[mpctrl thumbnailImageAtTime:playbackTime timeOption:MPMovieTimeOptionNearestKeyFrame]];
    UIImage *img=[mpctrl thumbnailImageAtTime:playbackTime timeOption:MPMovieTimeOptionNearestKeyFrame];
    NSLog(@"screen_shot:%@",img);
    thumbnailView.image=img;
    [mpctrl stop];
    [mpctrl release];
    mpctrl=nil;
    [secondView setHidden:NO];
    [firstView setHidden:YES];
}

-(void)takeVideo:(id)sender{
    if (!ipc){
        ipc=[[UIImagePickerController alloc]init];
        ipc.delegate=self;
    }
    isTaking=YES;
    ipc.sourceType=UIImagePickerControllerSourceTypeCamera;
    ipc.mediaTypes=[NSArray arrayWithObject:@"public.movie"];
    [self showview];
}
-(void)selectVideo:(id)sender{
    /*if (!ipc){
        ipc=[[UIImagePickerController alloc]init];
        ipc.delegate=self;
    }
    isTaking=NO;
    ipc.sourceType=UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    ipc.mediaTypes=[NSArray arrayWithObject:@"public.movie"];
    [self showview];*/
}
-(void)dealloc{
    [firstView release];
    [secondView release];
    [btnSelect release];
    [btnTake release];
    [lblSelect release];
    [lblTake release];
    [super dealloc];
}
/*
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    [self hideview];
    NSLog(@"didFinish");
}*/

// 图片保存回调
- (void)image:(UIImage *) image
    didFinishSavingWithError: (NSError *) error
                 contextInfo: (void *) contextInfo{
    //myImageView.image = image;
    NSLog(@"%@",error);
}

// 视频保存回调
- (void)video:(NSString *) videoPath
    didFinishSavingWithError: (NSError *) error
                 contextInfo: (void *) contextInfo{
    NSLog(@"%@",videoPath);
    NSLog(@"%@",error);
    //MyRequest *request=[[[MyRequest alloc]init]autorelease];
    //[request uploadVideoDataWithURL:@"http://114.80.151.69/oms/video-upload/e/enews/upload2.php" withVideoURL:videoPath];
    NSLog(@"upload_video_complete");
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    // 图片类型
    if ([[info objectForKey:UIImagePickerControllerMediaType] isEqualToString:@"public.image"]) {
        UIImage* image = [info objectForKey:UIImagePickerControllerOriginalImage];
        // 保存图片
        UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    }
    // 视频类型 kut什么的定义需要 MobileCoreServices.framework 支持
    else if ([[info objectForKey:UIImagePickerControllerMediaType] isEqualToString:@"public.movie"]) {
        //NSString* path = [[info objectForKey:UIImagePickerControllerMediaURL] path];
        NSURL *videoURL=[info objectForKey:UIImagePickerControllerMediaURL];
        // 保存视频
        //UISaveVideoAtPathToSavedPhotosAlbum(path, self, @selector(video:didFinishSavingWithError:contextInfo:), nil);
        // 如果想之后立刻调用UIVideoEditor,animated不能是YES。最好的还是dismiss结束后在调用editor。
        
        //上传视频
        /*MyRequest *request=[[[MyRequest alloc]init]autorelease];
        [request uploadVideoDataWithURL:@"http://114.80.151.69/oms/video-upload/e/enews/upload3.php" withVideoURL:path];*/
        [videoURL retain];
        [self getThumbnailFromVideo:videoURL atTime:1];
        videoAdress=videoURL;
        NSLog(@"upload_video_complete");
    }
    [self hideview];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [self hideview];
    NSLog(@"didcancel");
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/


-(void)showview{
    AppDelegate *app=(AppDelegate *)[[UIApplication sharedApplication] delegate];
    [app.navigationController presentModalViewController:ipc animated:YES];
}
-(void)hideview{
    AppDelegate *app=(AppDelegate *)[[UIApplication sharedApplication] delegate];
    [app.navigationController dismissModalViewControllerAnimated:YES];
}

@end
